package com.szosen.util;

import java.util.Calendar;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;

import com.szosen.jkzj.R;

/***
 * 日期选择对话框
 * 
 * @author jiqunpeng
 * 
 *         创建时间：2014-4-9 上午8:56:34
 */
public class DateSelectDialog extends AlertDialog.Builder {
	private DialogTask dialogTask;
	private DatePicker datePicker;

	public DateSelectDialog(Context context) {
		super(context);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_date_selector, null);
		datePicker = (DatePicker) view.findViewById(R.id.date_picker);
		this.setView(view);
	}

	public DateSelectDialog(Context context, DialogTask dialogTask,
			String posNote, String neuNote, String negNote) {
		this(context);
		this.dialogTask = dialogTask;
		this.setTitle("日期选择");
		this.setPositiveButton(posNote, positivelistener);
		this.setNeutralButton(neuNote, neutrallistener);
		this.setNegativeButton(negNote, null);
	}

	public DateSelectDialog(Context conext, DialogTask dialogTask) {
		this(conext, dialogTask, "保存", "清除", "取消");
	}

	private OnClickListener positivelistener = new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			int day = datePicker.getDayOfMonth();
			int month = datePicker.getMonth() + 1;
			int year = datePicker.getYear();
			final Calendar calendar = Calendar.getInstance();
			// final int mYear = calendar.get(Calendar.YEAR); // 获取当前年份
			// final int mMonth = calendar.get(Calendar.MONTH);// 获取当前月份
			// final int mDay = calendar.get(Calendar.DAY_OF_MONTH);//
			// 获取当前月份的日期号码
			// final int mHour = calendar.get(Calendar.HOUR_OF_DAY);// 获取当前的小时数
			// final int mMinute = calendar.get(Calendar.MINUTE);// 获取当前的分钟数
			// String mm = "00";
			// if(mMinute>=0&&mMinute<=10){
			// mm="0"+mMinute;
			// }else{
			// mm=mMinute+"";
			// }
			final int mSecond = calendar.get(Calendar.SECOND);// 获取当前的分钟数
			String mYear = "";
			String mMonth = "";
			String mDay = "";
			if (month >= 0 && month < 10) {
				mMonth = "0" + month;
			}else{
				mMonth = "" + month;
			}
			if (day >= 0 && day < 10) {
				mDay = "0" + day;
			}else{
				mDay=""+day;
			}
			mYear = year + "";
			// String date = String.format(Locale.CHINA, "%4d-%2d-%2d ",year,
			// month, day);
			String date = mYear + "-" + mMonth + "-" + mDay;
			dialogTask.process(date);
		}
	};
	private OnClickListener neutrallistener = new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialogTask.process("");
		}
	};

	/*
	 * 对话框确认时选择的任务
	 */
	public interface DialogTask {
		public void process(String date);
	}

}
